Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: Fizz Buzz

Problem Statement

Given an integer n, return a 1-indexed string array res where:

  • res[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • res[i] == "Fizz" if i is divisible by 3.
  • res[i] == "Buzz" if i is divisible by 5.
  • res[i] == i (as a string) if above all conditions are false.

Examples

  • Example 1:

    • Input: 4
    • Expected Output: ["1", "2", "Fizz", "4"]
    • Justification: 3 is divisible by 3, so it's replaced with "Fizz". The rest are not divisible by 3 or 5, so they remain unchanged.
  • Example 2:

    • Input: 7
    • Expected Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]
    • Justification: 3 and 6 are divisible by 3 (replaced with "Fizz"), 5 is divisible by 5 (replaced with "Buzz"), and the rest are printed as is.
  • Example 3:

    • Input: 16
    • Expected Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", "16"]
    • Justification: 3, 6, 9, and 12 are divisible by 3 ("Fizz"), 5 and 10 are divisible by 5 ("Buzz"), and 15 is divisible by both 3 and 5 ("FizzBuzz"). The rest are printed as is.

Solution

To solve this problem, we'll iterate through all numbers from 1 to n, inclusively. At each iteration, we will evaluate the current number against two conditions: is it a multiple of 3, and is it a multiple of 5? Based on these conditions, we'll decide whether to add the number itself, "Fizz", "Buzz", or "FizzBuzz" to our result list.

This approach works because it directly applies the divisibility rule, ensuring that we correctly categorize each number according to the problem's requirements. It's effective because it's straightforward, avoids unnecessary complexity, and directly maps to the problem's logic.

Step-by-step Algorithm

  • Initialize an empty list result to store the output strings.
  • Iterate through all numbers from 1 to n (inclusive).
    • For each number, check if it is divisible by both 3 and 5.
      • If true, append "FizzBuzz" to result.
    • Else, check if the number is divisible by 3.
      • If true, append "Fizz" to result.
    • Else, check if the number is divisible by 5.
      • If true, append "Buzz" to result.
    • If none of the above conditions are met, append the string representation of the current number to result.
  • Return the result list.

Algorithm Walkthrough

Let's walk through the algorithm with the input n = 16.

  1. Start with 1: Not divisible by 3 or 5, output "1".
  2. Move to 2: Not divisible by 3 or 5, output "2".
  3. At 3: Divisible by 3, output "Fizz".
  4. At 4: Not divisible by 3 or 5, output "4".
  5. At 5: Divisible by 5, output "Buzz".
  6. At 6: Divisible by 3, output "Fizz".
  7. At 7: Not divisible by 3 or 5, output "7".
  8. At 8: Not divisible by 3 or 5, output "8".
  9. At 9: Divisible by 3, output "Fizz".
  10. At 10: Divisible by 5, output "Buzz".
  11. At 11: Not divisible by 3 or 5, output "11".
  12. At 12: Divisible by 3, output "Fizz".
  13. At 13: Not divisible by 3 or 5, output "13".
  14. At 14: Not divisible by 3 or 5, output "14".
  15. At 15: Divisible by both 3 and 5, output "FizzBuzz".
  16. At 16: Not divisible by 3 or 5, output "16".

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

  • : The algorithm iterates once through all numbers from 1 to n, performing a constant amount of work for each number (checking divisibility by 3, 5, and both). Therefore, the time complexity is directly proportional to the input size n.

Space Complexity

  • : The space complexity is also linear with respect to the input size n, as we store each result (either "Fizz", "Buzz", "FizzBuzz", or the number itself as a string) in a list (or array, depending on the programming language) that grows with n.
Fizz Buzz (easy)
Increasing Order Search Tree (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity